home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Audio / MPEG / AMPDecoder.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-06  |  7.1 KB  |  303 lines

  1. /* 
  2.  *  AMPDecoder.cpp
  3.  *
  4.  *  Code from
  5.  *            NekoAmp 1.3 decoder by Avery Lee
  6.  *
  7.  *  FlasKMPEG
  8.  *    Copyright (C) Alberto Vigata - January 2000
  9.  *
  10.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  11.  *    
  12.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  13.  *  it under the terms of the GNU General Public License as published by
  14.  *  the Free Software Foundation; either version 2, or (at your option)
  15.  *  any later version.
  16.  *   
  17.  *  FlasKMPEG is distributed in the hope that it will be useful,
  18.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  *  GNU General Public License for more details.
  21.  *   
  22.  *  You should have received a copy of the GNU General Public License
  23.  *  along with GNU Make; see the file COPYING.  If not, write to
  24.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  25.  *
  26.  */
  27.  
  28. #include "AMPDecoder.h"
  29.  
  30. IAMPDecoder *CreateAMPDecoder() {
  31.     return new AMPDecoder();
  32. }
  33.  
  34. AMPDecoder::AMPDecoder() {
  35. }
  36.  
  37. AMPDecoder::~AMPDecoder() {
  38. }
  39.  
  40. void AMPDecoder::Destroy() {
  41.     delete this;
  42. }
  43.  
  44. char *AMPDecoder::GetAmpVersionString() {
  45.     return "NekoAmp 1.3, based on FreeAmp 1.1.0 source";
  46. }
  47.  
  48. char *AMPDecoder::getErrorString(int err) {
  49.     switch(err) {
  50.     case ERR_NONE:        return "no error";
  51.     case ERR_EOF:        return "unexpected end of stream";
  52.     case ERR_READ:        return "read error";
  53.     case ERR_MPEG25:    return "cannot process Fraunhofer-IIS MPEG 2.5 stream";
  54.     case ERR_LAYER1:    return "cannot process MPEG Layer I streams";
  55.     case ERR_FREEFORM:    return "cannot process free-form streams";
  56.     case ERR_SYNC:        return "sync error";
  57.     case ERR_INTERNAL:    return "**Internal Error**";
  58.     case ERR_INCOMPLETEFRAME:    return "not enough data to decode frame";
  59.     }
  60.  
  61.     return "unknown error";
  62. }
  63.  
  64. void AMPDecoder::Init() {
  65.     int i;
  66.     float *pp;
  67.  
  68.     winptr = 0;
  69.  
  70.     for(i=0; i<512; i++)
  71.         window[0][i] = window[1][i] = 0.0f;
  72.  
  73.     pp = (float *)prevblck;
  74.  
  75.     for(i=0; i<SBLIMIT*SSLIMIT*2; i++) {
  76.         *pp++ = 0.0f;
  77.     }
  78.  
  79.     resetbits(0);
  80.  
  81.     Initialize();
  82. }
  83.  
  84. void AMPDecoder::setSource(IAMPBitsource *pSource) {
  85.     this->pSource = pSource;
  86. }
  87.  
  88. void AMPDecoder::setDestination(short *psDest) {
  89.     this->psDest = psDest;
  90. }
  91.  
  92. long AMPDecoder::getSampleCount() {
  93.     return lSampleCount;
  94. }
  95.  
  96. void AMPDecoder::Reset() {
  97. }
  98.  
  99. //////////////////////////////////////
  100.  
  101. static bool isValidMPEGHeader(long hdr) {
  102.     // 0000F0FF 12 bits    sync mark
  103.     //
  104.     // 00000800  1 bit    version
  105.     // 00000600  2 bits    layer (3 = layer I, 2 = layer II, 1 = layer III)
  106.     // 00000100  1 bit    error protection (0 = enabled)
  107.     //
  108.     // 00F00000  4 bits    bitrate_index
  109.     // 000C0000  2 bits    sampling_freq
  110.     // 00020000  1 bit    padding
  111.     // 00010000  1 bit    extension
  112.     //
  113.     // C0000000  2 bits    mode (0=stereo, 1=joint stereo, 2=dual channel, 3=mono)
  114.     // 30000000  2 bits    mode_ext
  115.     // 08000000  1 bit    copyright
  116.     // 04000000  1 bit    original
  117.     // 03000000  2 bits    emphasis
  118.  
  119.     // 00 for layer ("layer 4") is not valid
  120.     if (!(hdr & 0x00000600))
  121.         return false;
  122.  
  123.     // 1111 for bitrate is not valid
  124.     if ((hdr & 0x00F00000) == 0x00F00000)
  125.         return false;
  126.  
  127.     // 11 for sampling frequency is not valid
  128.     if ((hdr & 0x000C0000) == 0x000C0000)
  129.         return false;
  130.  
  131.     // Looks okay to me...
  132.     return true;
  133. }
  134.  
  135.  
  136. static const int bitrates2[3][15] = {
  137.     {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, },
  138.     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, },
  139.     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, },
  140. };
  141.  
  142. static const int bitrates[3][15] = {
  143.           {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448},
  144.           {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384},
  145.           {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320}
  146.         };
  147.  
  148. static const long samp_freq[4] = {44100, 48000, 32000, 0};
  149.  
  150. void AMPDecoder::ReadHeader() {
  151.     union {
  152.         char buf[4];
  153.         long hdr;
  154.     };
  155.  
  156.     // MPEG headers are 4 bytes.
  157.  
  158.     if (4 != pSource->read(buf, 4))
  159.         throw (int)ERR_EOF;
  160.  
  161.     // Is it a valid header?  If not, figure out how many
  162.     // bytes obviously aren't sync and advance by that much.
  163.  
  164.     while(!isValidMPEGHeader(hdr)) {
  165.         int advance = 1;
  166.  
  167.         if (buf[1] != 0xFF || (buf[2]&0xF0) != 0xF0) {
  168.             if (buf[2] != 0xFF || (buf[3]&0xF0) != 0xF0) {
  169.                 if (buf[3] != 0xFF)
  170.                     advance = 4;
  171.                 else
  172.                     advance = 3;
  173.             } else
  174.                 advance = 2;
  175.         }
  176.  
  177.         *(long *)buf >>= advance<<3;
  178.  
  179.         if (advance != pSource->read(buf+4-advance, advance))
  180.             throw (int)ERR_EOF;
  181.     }
  182.  
  183.     // Yay!  A valid MPEG header!  Parse it!
  184.  
  185.     // 0000F0FF 12 bits    sync mark
  186.     //
  187.     // 00000800  1 bit    version
  188.     // 00000600  2 bits    layer (3 = layer I, 2 = layer II, 1 = layer III)
  189.     // 00000100  1 bit    error protection (0 = enabled)
  190.     //
  191.     // 00F00000  4 bits    bitrate_index
  192.     // 000C0000  2 bits    sampling_freq
  193.     // 00020000  1 bit    padding
  194.     // 00010000  1 bit    extension
  195.     //
  196.     // C0000000  2 bits    mode (0=stereo, 1=joint stereo, 2=dual channel, 3=mono)
  197.     // 30000000  2 bits    mode_ext
  198.     // 08000000  1 bit    copyright
  199.     // 04000000  1 bit    original
  200.     // 03000000  2 bits    emphasis
  201.  
  202.     is_mpeg2            = !(hdr & 0x00000800);
  203.     layer                = 4 - (hdr>>9)&3;
  204.     is_errorprotected    = !(hdr & 0x00000100);
  205.     br_index            = (hdr>>20)&15;
  206.     bitrate                = (is_mpeg2 ? bitrates2 : bitrates)[layer-1][br_index];
  207.     sr_index            = (hdr>>18)&3;
  208.     frequency            = samp_freq[sr_index];
  209.  
  210.     if (is_mpeg2)
  211.         frequency>>=1;
  212.  
  213.     is_padded            = !!(hdr & 0x00020000);
  214.     is_extended            = !!(hdr & 0x00010000);
  215.     mode                = (hdr>>30)&3;
  216.     mode_ext            = (hdr>>28)&3;
  217.     is_copyrighted        = !!(hdr & 0x08000000);
  218.     is_original            = !!(hdr & 0x04000000);
  219.     emphasis            = (hdr>>24)&3;
  220.  
  221.     if (mode == MODE_MONO)
  222.         channels = 1;
  223.     else
  224.         channels = 2;
  225.  
  226.     // Urk!  We don't accept Layer I...
  227.  
  228.     if (layer == 1)
  229.         throw (int)ERR_LAYER1;
  230.  
  231.     // Compute the frame size, not including header and layer III side info
  232.  
  233.     if (is_mpeg2)
  234.         frame_size = 72000 * bitrate / frequency;
  235.     else
  236.         frame_size = 144000 * bitrate / frequency;
  237.  
  238.     if (is_padded)
  239.         ++frame_size;
  240.  
  241.     frame_size -= 4;
  242.  
  243.     // if we're processing Layer III, subtract size of side info as well
  244.  
  245.     if (layer == 3) {
  246.         if (is_mpeg2)
  247.             if (mode == MODE_MONO)
  248.                 sideinfo_size = 9;
  249.             else
  250.                 sideinfo_size = 17;
  251.         else
  252.             if (mode == MODE_MONO)
  253.                 sideinfo_size = 17;
  254.             else
  255.                 sideinfo_size = 32;
  256.  
  257.         frame_size -= sideinfo_size;
  258.     }
  259.  
  260.     // Read in CRC if we have error protection
  261.  
  262.     if (is_errorprotected) {
  263.         unsigned short CRC;
  264.  
  265.         if (2 != pSource->read(&CRC, 2))
  266.             throw (int)ERR_READ;
  267.  
  268.         frame_size -= 2;
  269.     }
  270.  
  271.     // Read in Layer3 side info
  272.  
  273.     if (layer == 3)
  274.         L3_GetSideInfo();
  275.  
  276. }
  277.  
  278. void AMPDecoder::getStreamInfo(AMPStreamInfo *pasi) {
  279.     pasi->lBitrate        = bitrate;
  280.     pasi->lSamplingFreq    = frequency;
  281.     pasi->nLayer        = layer;
  282.     pasi->nMPEGVer        = is_mpeg2 ? 2 : 1;
  283.     pasi->fStereo        = mode != MODE_MONO;
  284. }
  285.  
  286. void AMPDecoder::PrereadFrame() {
  287.     if (layer == 2)
  288.         L2_PrereadFrame();
  289.     else if (layer == 3)
  290.         L3_PrereadFrame();
  291. }
  292.  
  293. bool AMPDecoder::DecodeFrame() {
  294.     lSampleCount = 0;
  295.  
  296.     if (layer == 2)
  297.         return L2_DecodeFrame();
  298.     else if (layer == 3)
  299.         return L3_DecodeFrame();
  300.  
  301.     return false;
  302. }
  303.